package com.study.cloud.utils;

import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import reactor.util.function.Tuple2;
import reactor.util.function.Tuples;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Date 2022/5/16
 * @Author Cliff
 * desc
 */
public class SplitStringUtil {

    public static JSONObject splitFormStr(String content) {
        JSONObject jsonObject = new JSONObject();
        if (StringUtils.isEmpty(content)) {
            return jsonObject;
        }
        int index = content.indexOf("\r\n----");
        if (index < 0) {
            jsonObject.put("content", content);
            return jsonObject;
        }
        List<String> oneList = Splitter.on("\r\n----").splitToList(content);
        if (CollectionUtils.isEmpty(oneList)) {
            jsonObject.put("content", content);
            return jsonObject;
        }
        for (int i = 0; i < oneList.size() - 1; i++) {
            String info = oneList.get(i);
            String name = "name=\"";
            int j = info.indexOf("name=");
            String substring = info.substring(j);
            int y = substring.indexOf("name=");
            String sub = substring.substring(y + name.length());
            ArrayList<String> list = Lists.newArrayList(Splitter.on("\"\r\n\r\n").split(sub));
            jsonObject.put(list.get(0), list.get(1));
        }
        return jsonObject;
    }

    public static JSONObject splitWWWFormStr(String content) {
        JSONObject jsonObject = new JSONObject();
        if (StringUtils.isEmpty(content)) {
            return jsonObject;
        }
        int index = content.indexOf("&");
        if (index < 0) {
            if (content.indexOf("=") > 0) {
                List<String> toList = Splitter.on("=").splitToList(content);
                jsonObject.put(toList.get(0), toList.get(1));
                return jsonObject;
            }
            jsonObject.put("content", content);
            return jsonObject;
        }
        ArrayList<String> list = Lists.newArrayList(Splitter.on("&").split(content));
        if (CollectionUtils.isEmpty(list)) {
            jsonObject.put("content", content);
            return jsonObject;
        }
        Map<String, Object> collect = list.stream().map(info -> {
            List<String> toList = Splitter.on("=").splitToList(info);
            return Tuples.of(toList.get(0), toList.get(1));
        }).collect(Collectors.toMap(Tuple2::getT1, Tuple2::getT2));

        jsonObject.putAll(collect);

        return jsonObject;
    }

    /**
     * 将map参数转换成url参数
     * @param queryParams
     * @return
     */
    public static String getUrlParamsByMap(Map<String, String> queryParams) {
        JSONObject jsonObject = new JSONObject();
        if (ObjectUtils.isEmpty(queryParams)) {
            return jsonObject.toJSONString();
        }
        queryParams.forEach(jsonObject::put);
        return jsonObject.toJSONString();
    }
}
